
#include "sqlite3.h"
#define DLL_EXPORT
#include "lbsqlite.h"
#include <iomanip>
int LBsqlite::_colCount=0;
int LBsqlite::_rowCount=0;
struct  tables
{
   std::string collName;
   std::string rowData;
  
} myTable;
  int rows=0;//--记录行数
  sqlite3 *sqliteDB;
  std::vector<tables> sqliteTabels;
int callback(void *NotUsed, int argc, char **argv, char **azColName){
      LBsqlite::colCount(argc);
       rows=rows+1;
     for(int i=0; i<argc; i++){
        myTable.collName=azColName[i];
        myTable.rowData=argv[i] ? argv[i] : "NULL";
        sqliteTabels.push_back(myTable);
      
       }
    LBsqlite::rowCount(rows);
    return 0;
}
//-------------------------------下是字符串处理类---
class lbstring
{
private:
   char *_p=nullptr;
public:
    lbstring()=default;
    ~lbstring();
    const char *getString(const char *str);
    static lbstring *lbInterface();
};
lbstring::~lbstring()
{
    if(_p)
    {
        delete []_p;
        _p=nullptr;
        #ifdef __DEBUG__
         std::cout<<"lbstring::~lbstring()"<<std::endl;
        #endif
    }
}

const char *lbstring::getString(const char *str)
{
    _p=new char[strlen(str)+1];
    strcpy(_p,str);
    return _p;
}
lbstring *lbstring::lbInterface()
{
    static lbstring mystring;
    return &mystring;
}
//-------------------------------以上是字符串处理类---------
std::vector<std::string> LBsqlite::getFiledValue(const int col)
{
     int cols=GetcolCount();
     std::vector <std::string> fieldValue;
     if(col>cols){
         return fieldValue;
      }
    int step=col;
    for(int i=0;i<sqliteTabels.size();i++)
    {
      if(i==step){
       fieldValue.push_back(sqliteTabels.at(step).rowData);
       step=step+cols;
       }
      }
      
 return fieldValue;
}
LBsqlite::LBsqlite()
{
}
//---------------------------------------以上是公共部分----
LBsqlite::~LBsqlite()
{
   
   sqlite3_close(sqliteDB);
   sqlite3_free(sqliteError);

}
//-------------------------------
LBsqlite::LBsqlite(const char *fileName)
{
   _fileName=fileName;
    // sqlite3 *p=Lsqlite::init().DB();
    rc = sqlite3_open(fileName, &sqliteDB);
    if(rc!=SQLITE_OK)
    {
       msgERR="sqlite3 open Error"; 
    }
}

bool LBsqlite::open(const char *fileName)
{
     bool ok;
      _fileName=fileName;
     // sqlite3 *p=Lsqlite::init().DB();
     rc = sqlite3_open(fileName, &sqliteDB);
     if(rc==SQLITE_OK)
     {
         ok=true;
     }
    else
    {
         ok=false;
         msgERR="LBsqlite::open:sqlite3 open Error"; 
    }
     
     return ok;
}

const char *LBsqlite::getLastError()
{
  return msgERR.c_str();
}

bool LBsqlite::execSql(const char *sql)
{
     sqliteTabels.clear();
     rows=0;
   //  sqlite3 *p=Lsqlite::init().DB(); 
    if(sqlite3_exec(sqliteDB, sql, callback, 0, &sqliteError)==SQLITE_OK) 
    {
        return true;
    }
   else
   {
     msgERR=sqliteError;
     return false;  
   }
      
}

void LBsqlite::showTables()
{
 //std::cout<< "----------------------------------------"<<std::endl;
 int row=GetrowCount();
 int col=GetcolCount();
 for (int i = 0; i < col; ++i) {
        // 输出每个 tables 结构体对象的信息
    std::cout << std::left<<std::setw(10)<<sqliteTabels.at(i).collName<<"  ";
    
   }
   std::cout<< std::endl;
  // std::cout <<"-------------------------------------------------------"<<std::endl;
    for (size_t r = 0; r < sqliteTabels.size(); ++r) 
    {
        // 输出每个 tables 结构体对象的信息
        std::cout << std::left<<std::setw(10)<<sqliteTabels.at(r).rowData <<"  " ;
        if((r+1)%col==0)  std::cout <<std::endl;
    }

}

int  LBsqlite::GetcolCount()
{
  return _colCount;
}

int LBsqlite::GetrowCount()
{
    return _rowCount;
}

 void LBsqlite::colCount(int col)
{
    _colCount=col;
}

void LBsqlite::rowCount(int row)
{
    _rowCount=row;
}

const char *LBsqlite::execValue(const int num)
{
   std::vector <std::string> FiledValue;
   FiledValue=getFiledValue(num);
   return lbstring::lbInterface()->getString(FiledValue.at(_res-1).c_str());
}

const char *LBsqlite::execValue(const char *filed)
{
    int col=GetcolCount();
    std::string _filed=filed;
    int num=0;
    for(int i=0;i<col;i++)
    {
        if(_filed==sqliteTabels.at(i).collName)
        {
            num=i;
            break;
        }
    }
  return execValue(num);
}

void LBsqlite::setValue(const char *tableName, const int indx)
{
   //----tableName 表名 ，indx 字段索引------------------
    //  char  *sql=nullptr;
    //  sprintf(sql,"update %s set %s=%s",tableName,);
    //  std::string sql="update"+*tableName;
    //  this->execSql("");


}

bool LBsqlite::toNext()
{
   if(_res<GetrowCount())
   {
     _res=_res+1;
      return true;
   }
 
   return false;
}

void LBsqlite::toFirst()
{
  _res=1;
}

void LBsqlite::toLast()
{
     last=true;
    _res=GetrowCount();
}

bool LBsqlite::toPrev()
{
  if(last){
      last=false;
      return true;
    }
 if(_res>1)
   {
      last=false;
      _res=_res-1;
      return true;
   }
 
   return false;
}

LBsqlite *LBsqlite::DBinit()
 {
         static LBsqlite _DB;
         return &_DB;
}

int main()
{
     if(936==GetConsoleOutputCP())        //--判断当前系统是否为UTF-8 
       {                                  //#define CP_UTF8 65001 utf-8   //SetConsoleOutputCP(936); GBK
         SetConsoleOutputCP(CP_UTF8); 
       }
    
    LBsqlite mysqlite;
    bool ok=mysqlite.open("test.db");
    if(ok)
    {
        std::cout<<"打开成功！"<<std::endl;
    }
    mysqlite.execSql("select * from COMPANY where id>0");
    std::cout<< mysqlite.getLastError()<<std::endl;;
    mysqlite.showTables();
    std::cout<<"----------------------------"<<std::endl;
    while (mysqlite.toNext()){
        std::cout<<std::setw(10)<<mysqlite.execValue(0)\
              <<std::setw(10)<<mysqlite.execValue(1)\
              <<std::setw(10)<<mysqlite.execValue(4)\
              <<std::setw(10)<<mysqlite.execValue("NAME")<<std::endl;
         
        }
std::cout<<"---------------------------***********"<<std::endl;
        mysqlite.toFirst();
        std::cout<<mysqlite.execValue(0)<<"\n";
        mysqlite.toLast();
        std::cout<<mysqlite.execValue(0)<<"\n";
 std::cout<<"---------------------------***********"<<std::endl;
    mysqlite.toLast();
     while (mysqlite.toPrev()){
              std::cout<<std::setw(10)<<mysqlite.execValue(0)\
              <<std::setw(10)<<mysqlite.execValue(1)\
              <<std::setw(10)<<mysqlite.execValue(4)\
              <<std::setw(10)<<mysqlite.execValue("NAME")<<std::endl;
         
        }
    
    
    return 0;
}
